'************************************************************************************
' Copyright © 2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
Option Strict On
Option Explicit On 

Imports System

Namespace KADGen.Utility
   Public Class DebugTools
      Private Const vbTab As String = Microsoft.VisualBasic.ControlChars.Tab
      Private Const vbCrLf As String = Microsoft.VisualBasic.ControlChars.CrLf

      Public Shared Sub Ping()

      End Sub


      Public Shared Function DisplayRows(ByVal dataTable As Data.DataTable) As String
         Dim sb As New Text.StringBuilder
         DisplayRowHeader(sb, dataTable)
         sb.Append(vbCrLf)
         For Each row As Data.DataRow In dataTable.Rows
            DisplayRow(sb, row)
            sb.Append(vbCrLf)
         Next
         sb.Append(vbCrLf)
         sb.Append("Rows: " & dataTable.Rows.Count.ToString & vbTab & "Columns: " & dataTable.Columns.Count.ToString)
         Return sb.ToString
      End Function

      Public Shared Function DisplayRow(ByVal dataRow As Data.DataRow) As String
         Dim sb As New Text.StringBuilder
         DisplayRowHeader(sb, dataRow.Table)
         DisplayRow(sb, dataRow)
         Return sb.ToString
      End Function

      Private Shared Sub DisplayRowHeader( _
               ByVal sb As Text.StringBuilder, _
               ByVal dataTable As Data.DataTable)
         For Each column As Data.DataColumn In dataTable.Columns
            sb.Append(column.ColumnName & vbTab)
         Next
         sb.Append(vbCrLf)
      End Sub

      Private Shared Sub DisplayRow( _
               ByVal sb As Text.StringBuilder, _
               ByVal dataRow As Data.DataRow)
         Dim dt As Data.DataTable = dataRow.Table
         For i As Int32 = 0 To dt.Columns.Count - 1
            sb.Append(dataRow(i).ToString & vbTab)
         Next
      End Sub

      Public Shared Function DisplayStream( _
               ByVal stream As IO.Stream) _
               As String
         Return DisplayStream(stream, False)
      End Function

      Public Shared Function DisplayStream( _
               ByVal stream As IO.Stream, _
               ByVal rewind As Boolean) _
               As String
         Dim streamreader As IO.StreamReader
         Dim pos As Int64
         Dim ret As String
         pos = stream.Position
         streamreader = New IO.StreamReader(stream)
         If rewind Then
            stream.Position = 0
         End If
         ret = streamreader.ReadToEnd
         stream.Position = pos
         Return ret
      End Function

      Public Shared Function DisplayNamespaceManager( _
                       ByVal nsmgr As Xml.XmlNamespaceManager) _
                       As String
         Dim s As String = String.Empty
         For Each prefix As String In nsmgr
            s &= prefix & ":" & nsmgr.LookupNamespace(prefix) & Environment.NewLine
         Next prefix
         Return s
      End Function

      Public Shared Function DisplayEnvironmentVariables() As String
         Dim vars As Collections.IDictionary = System.Environment.GetEnvironmentVariables()
         Dim s As String = ""
         For Each var As Collections.DictionaryEntry In vars
            s &= var.Key.ToString() & " : " & var.Value.ToString() & vbCrLf
         Next
         Return s
      End Function

   End Class

End Namespace